package org.veridiandynamics.client.view;

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.veridiandynamics.server.dao.HibernateDriver;
import org.veridiandynamics.server.dao.Queryprocessor;
import org.veridiandynamics.server.domain.Patient;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * This panel handles the deletion of patient's from the database.
 * 
 * @author Lee
 */
public class DeletePatientPanel extends AbstractPanelLayout 
{
	private JButton back;
	private JLabel lblPleaseEnterThe_1;
	private JTextField textField_2;
	private JButton submit;
	private Map<String, Object> deletePatientMap;

	/**
	 * The constructor for the panel.  Initializes all the widgets on the panel.
	 * 
	 * @param c1
	 * @param container
	 */
	public DeletePatientPanel(final CardLayout c1, final Container container)
	{
		numCenterPanels = 1;
		initialize();
		panelLeft.setLayout(new BoxLayout(panelLeft, BoxLayout.Y_AXIS));
		MainWindow.welcomeMessage.setText("Delete Patient");

		JPanel subLeft = new JPanel();
		subLeft.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(89dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(0dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(91dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(19dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));

		lblPleaseEnterThe_1 = new JLabel("Please Enter the Patient's Username:");
		subLeft.add(lblPleaseEnterThe_1, "4, 4");

		panelLeft.add(subLeft);

		textField_2 = new JTextField();
		subLeft.add(textField_2, "4, 6, fill, default");
		textField_2.setColumns(10);

		JPanel subLeft2 = new JPanel();
		submit = new JButton("Submit");
		back = new JButton("Back");
		back.addActionListener(new ActionListener() 
		{
			/**
			 * This method takes the user back to the nurse panel.
			 */
			public void actionPerformed(ActionEvent event) 
			{
				c1.first(container);
				MainWindow.welcomeMessage.setText("Welcome");
			}
		});
		subLeft2.add(submit);
		submit.addActionListener(new ActionListener() 
		{
			/**
			 * The method listens to the submit button on the panel and 
			 * handles the deletion of the patient.
			 */
			public void actionPerformed(ActionEvent event) 
			{
				if (!(textField_2.getText().equals("")))
				{	
					//					String username = textField_2.getText();
					//					Map<String, Object> deleteMap = new HashMap<String, Object>();
					//					deleteMap.put("requestType", "deletePatient");
					//					deleteMap.put("username", username);

					String username = textField_2.getText();
					HibernateDriver driver = new HibernateDriver();
					long id = Queryprocessor.UsernametoID(username);
					Patient patient = new Patient();
					patient = (Patient) driver.RetrieveObject(id, patient);
					driver.DeleteObject(patient);

					JOptionPane.showMessageDialog(null, "Patient: was deleted");
				}

				else JOptionPane.showMessageDialog(null, "Please fill out all forms");
			}
		});
		subLeft2.add(back);
		panelLeft.add(subLeft2);
	}

}
